package com.medi.business.service.impl;

import com.medi.business.domain.DataCondition;
import com.medi.business.domain.DataRelation;
import com.medi.business.domain.vo.DataAuthorizeVo;
import com.medi.business.domain.vo.DataRelationVo;
import com.medi.business.mapper.DataConditionMapper;
import com.medi.business.mapper.DataRelationMapper;
import com.medi.business.service.IDataAuthorizeService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据权限关系Service业务层处理
 *
 * @author medi
 * @date 2024-09-13
 */
@Service
public class DataAuthorizeServiceImpl implements IDataAuthorizeService {
    @Autowired
    private DataRelationMapper dataRelationMapper;

    @Autowired
    private DataConditionMapper dataConditionMapper;

    /**
     * 查询数据权限关系
     *
     * @param id 数据权限关系主键
     * @return 数据权限关系
     */
    @Override
    @SneakyThrows
    public DataAuthorizeVo selectDataAuthorizeVoById(String id) {
        DataAuthorizeVo dataAuthorizeVo = new DataAuthorizeVo();
        DataRelation dataRelation = new DataRelation();
        dataRelation.setId(id);
        List<DataRelationVo> list = dataRelationMapper.selectDataRelationVoList(dataRelation);
        if (list != null && list.size() > 0) {
            dataAuthorizeVo.setRelation(list.get(0));
        }
        DataCondition dataCondition = new DataCondition();
        dataCondition.setDataRelationId(id);
        List<DataCondition> conditionList = dataConditionMapper.selectDataConditionList(new DataCondition());
        if (conditionList != null && conditionList.size() > 0) {
            dataAuthorizeVo.setConditionList(conditionList);
        }
        return dataAuthorizeVo;
    }


    /**
     * 新增数据权限关系
     *
     * @param dataAuthorizeVo 数据权限关系
     * @return 结果
     */
    @Override
    @SneakyThrows
    @Transactional
    public int insertDataAuthorize(DataAuthorizeVo dataAuthorizeVo) {
        if (ObjectUtils.isEmpty(dataAuthorizeVo)) {
            throw new Exception("参数为空");
        }
        DataRelationVo dataRelationVo = dataAuthorizeVo.getRelation();
        if (ObjectUtils.isEmpty(dataRelationVo)) {
            throw new Exception("业务数据关联信息参数为空");
        }
        DataRelation dataRelation = new DataRelation();
        BeanUtils.copyProperties(dataRelationVo, dataRelation);
        List<DataCondition> conditionList = dataAuthorizeVo.getConditionList();
        if (ObjectUtils.isEmpty(conditionList) || conditionList.size() <= 0) {
            throw new Exception("业务数据筛选条件参数为空");
        }
        dataRelation.creat();
        int i = dataRelationMapper.insertDataRelation(dataRelation);
        if(i>0)
        {
            for (DataCondition dataCondition : conditionList) {
                dataCondition.setDataRelationId(dataRelation.getId());
                dataCondition.creat();
                dataConditionMapper.insertDataCondition(dataCondition);
            }
        }
        return i;
    }

    /**
     * 数据权限过滤规则
     *
     * @param dataAuthorizeVo 数据权限过滤规则
     * @return 结果
     */
    @Override
    @SneakyThrows
    @Transactional
    public int updateDataAuthorize(DataAuthorizeVo dataAuthorizeVo) {
        if (ObjectUtils.isEmpty(dataAuthorizeVo)) {
            throw new Exception("参数为空");
        }
        DataRelationVo dataRelationVo = dataAuthorizeVo.getRelation();
        if (ObjectUtils.isEmpty(dataRelationVo)) {
            throw new Exception("业务数据关联信息参数为空");
        }
        DataRelation dataRelation = new DataRelation();
        BeanUtils.copyProperties(dataRelationVo, dataRelation);
        List<DataCondition> conditionList = dataAuthorizeVo.getConditionList();
        if (ObjectUtils.isEmpty(conditionList) || conditionList.size() <= 0) {
            throw new Exception("业务数据筛选条件参数为空");
        }
        dataRelation.modif();
        int i = dataRelationMapper.updateDataRelation(dataRelation);
        if(i>0)
        {
            dataConditionMapper.deleteDataConditionByRelationId(dataRelation.getId());
            for (DataCondition dataCondition : conditionList) {
                dataCondition.setDataRelationId(dataRelation.getId());
                dataCondition.creat();
                dataConditionMapper.insertDataCondition(dataCondition);
            }
        }
        return i;
    }

    /**
     * 删除数据权限关系信息
     *
     * @param id 数据权限关系主键
     * @return 结果
     */
    @Override
    public int deleteDataAuthorizeById(String id) {
        return dataRelationMapper.deleteDataRelationById(id);
    }
}
